<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<title>Title</title>
	<link rel="stylesheet" href="../../../component/pear/css/pear.css" />
	<script src="../../../component/layui/layui.js"></script>
	<script src="../../../component/pear/pear.js"></script>
	<script src="../../../component/plug/xm-select.js"></script>
</head>

<body>
	<form class="layui-form" action="" lay-filter="edit">
		<div class="mainBox">
			<div class="main-container">
				<div class="main-container">
					<div class="layui-form-item">
						<label class="layui-form-label">账号</label>
						<div class="layui-input-block">
							<input type="text" name="account" lay-verify="account" autocomplete="off"
								class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">密码</label>
						<div class="layui-input-block">
							<input type="password" name="password" lay-verify="password" autocomplete="off"
								placeholder="请输入密码，不输入即不修改密码" class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">角色</label>
						<div class="layui-input-block">
							<div name="roleIds" id="roleIds" class="xm-select-demo">

							</div>
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">状态</label>
						<div class="layui-input-block">
							<input type="checkbox" name="isActive" lay-skin="switch" lay-text="启用|禁用" checked>
						</div>
					</div>
				</div>
			</div>
		</div>
		<div class="bottom">
			<div class="button-container">
				<button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="save">
					<i class="layui-icon layui-icon-ok"></i>
					提交
				</button>
				<button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
					<i class="layui-icon layui-icon-refresh"></i>
					重置
				</button>
			</div>
		</div>
	</form>
</body>

</html>

<script>
	layui.use(['form', 'jquery', 'context', 'permission', 'popup', 'common', 'button'], function () {
		let form = layui.form;
		let $ = layui.jquery;
		let context = layui.context;
		let popup = layui.popup;
		let common = layui.common;
		let button = layui.button;

		let basePath = context.get("base-path");

		let currentId = common.getQueryString("id");

		//加载角色多选下拉框
		var roleXmSelect = common.loadXmSelect({
			divId: "#roleIds",
			name: "roleIds",
			url: basePath + '/api/system/role/getlist',
			valueField: "id",
			textField: "name",
			filterable: true
		})
		$.ajax({
			url: basePath + '/api/system/user/getinfo',
			data: JSON.stringify({ id: currentId }),
			success: function (result) {
				form.val("edit", result.data);//表单赋值
				roleXmSelect.setValue(result.data.roleIds)
			}
		})

		form.on('submit(save)', function (data) {
			var btn = button.load({ elem: 'button[lay-filter=save]' });
			data.field.id = currentId
			data.field.isActive = data.field.isActive == "on" ? true : false;
			$.ajax({
				url: basePath + '/api/system/user/edit',
				data: JSON.stringify(data.field),
				success: function (result) {
					btn.stop(function () {
						if (result.succeeded) {
							popup.success("修改成功", function () {
								parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
								parent.layui.table.reload("table");
							});
						} else {
							popup.failure(JSON.stringify(result.errors));
						}
					})
				}
			})
			return false;
		});
	})
</script>